package com.joe.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

public class BeanUtils {

    private BeanUtils(){
    }

    public static Map<String,Object> bean2Map(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String,Object> map = mapper.readValue(mapper.writeValueAsString(obj), Map.class);
        // TODO 删除value为空的键值对
        return map;
    }


    public static <T> T map2Bean(Map<String,Object> map, Class<T> cla) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(mapper.writeValueAsString(map), cla);
    }

}
